export default {

    clearAll() {
        window.sessionStorage.clear();
    },

    saveToken(token) {
        window.sessionStorage.setItem("token", token);
    },

    getToken() {
        return window.sessionStorage.getItem("token");
    },

    getUser() {
        let r = window.sessionStorage.getItem("user");

        if (r != undefined && r != 'undefined') {
            return JSON.parse(r);
        }
        return null;
    },

    getRoles() {
        let r = window.sessionStorage.getItem("roles");

        if (r != undefined && r != 'undefined') {
            return JSON.parse(r);
        }
        return [];
    },

    getPermissions() {
        let p = window.sessionStorage.getItem("permissions");

        if (p != undefined && p != 'undefined') {

            return JSON.parse(p);
        }
        return [];
    },

    saveAuthenticationInfo(info) {

        let user = info.user;

        let roles = info.roles;

        let permissions = info.permissions;

        window.sessionStorage.setItem("user", JSON.stringify(user));
        window.sessionStorage.setItem("roles", JSON.stringify(roles));
        window.sessionStorage.setItem("permissions", JSON.stringify(permissions));
    }


    ,
    getDictItem(ident, key, s, f) {

        let req = window.indexedDB.open('dict', 1);

        req.onupgradeneeded = (e) => {
            let db = e.target.result;
            db.createObjectStore('dict');
        }

        req.onerror = (e) => {
            if (f) {
                f(e);
            }

        }

        req.onsuccess = (e) => {

            let db = e.target.result;

            let t = db.transaction('dict', 'readwrite');
            let store = t.objectStore("dict");

            let r = store.get(ident);
            r.onsuccess = () => {
                if (s) {

                    if(Array.isArray(r.result)){
                        for(let item of r.result){

                            if(item.itemKey === key){

                                s(item);
                                return;
                            }
                        }
                        if(f)
                            f();
                    }else{
                        if(f)
                            f();
                    }
                }

                db.close()
            }
            r.onerror = () => {
                if (f) {

                    f(r.error);
                }

                db.close()
            }

        }

    }
    ,
    getDictItems(ident, s, f) {

        let req = window.indexedDB.open('dict', 1);

        req.onupgradeneeded = (e) => {
            let db = e.target.result;
            db.createObjectStore('dict');
        }

        req.onerror = (e) => {
            if (f) {
                f(e);
            }
        }

        req.onsuccess = (e) => {

            let db = e.target.result;

            let t = db.transaction('dict', 'readwrite');
            let store = t.objectStore("dict");

            let r = store.get(ident);
            r.onsuccess = () => {
                if (s) {
                    s(r.result);
                }

                db.close()
            }
            r.onerror = () => {
                if (f) {

                    f(r.error);
                }
                db.close()
            }

        }
    }
    ,
    saveDictItems(ident, items) {

        let req = window.indexedDB.open('dict', 1);

        req.onupgradeneeded = (e) => {
            let db = e.target.result;
            db.createObjectStore('dict');
        }

        req.onerror = (e) => {

        }

        req.onsuccess = (e) => {

            let db = e.target.result;

            let store = db.transaction('dict', 'readwrite').objectStore("dict");

            store.put(items, ident );

            db.close()
        }

    },

    clearDicts(){

        let req = window.indexedDB.open('dict', 1);
        req.onupgradeneeded = (e) => {
            let db = e.target.result;
            db.createObjectStore('dict');
        }
        req.onerror = (e) => {

        }

        req.onsuccess = (e) => {

            let db = e.target.result;

            let store = db.transaction('dict', 'readwrite').objectStore("dict")

            if(store){
                store.clear()
            }

            db.close()

        }
    }


}
